package com.vcms.view.simulation.customer;

import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;

import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.entity.store.item.StoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.utility.formatter.MoneyFormatter;
import com.vcms.view.simulation.uicomponent.listener.DrinkSelectionListener;

@SuppressWarnings("serial")
public class NumericDrinkSelectionBox extends DrinkSelectionBox {
	
	Button[] buttons;
	String previousCommand = "";

	public NumericDrinkSelectionBox(Container composite, TransactionController tCtrl) {
		super(composite, tCtrl);
		this.initDrinkSelection();
		this.setLayout(new GridLayout(0, 1));
	}
	
	public void initDrinkSelection() {
		super.initDrinkSelection();
	}

	public void initUI() {
		StoreItem[] items = transactionController.getMainControler()
				.getStoreController().getStoreItems(StoreController.DRINK);

		drinkId = new int[items.length];

		Panel tp = new Panel(new GridLayout(drinkId.length, 1));
		drinkSelection = new NumericDrinkSelection[drinkId.length + 1];

		for (int i = 0; i < items.length; i++) {
			DrinksBrand ob = (DrinksBrand) items[i].getContent();
			drinkSelection[i] = new NumericDrinkSelection();
			update( i, items[i].getQuantity(), ob.getPrice(), ob.getName() );
			drinkId[i] = i;
			tp.add(drinkSelection[i]);
		}
		this.add(tp);
		initButtonPanel();
	}
	
	protected void setAction() {
		int i;
		for (i = 0; i < drinkId.length; i++) {
			((NumericDrinkSelection)drinkSelection[i]).setNumber(intToString(i));
		}
		for (i = 0; i < buttons.length-1; i++) {
			buttons[i].setActionCommand(String.valueOf(i + 1));
		}
		buttons[buttons.length-1].setActionCommand(String.valueOf(0));
	}
	
	public void update(int brandIndex, int quantity, int price, String name){
		super.update(brandIndex, quantity, price, name);
	}
	
	public void setActive(boolean active){
		super.setActive(active);
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setEnabled(active);
		}
	}
	
	protected String intToString(int index) {
		int first = (int)((index + 1)  / (buttons.length-1));
		int second = (index + 1) % (buttons.length-1);
		return String.valueOf(first).concat(String.valueOf(second));
	}
	
	protected int stringToInt(String input) {
		int first = Integer.parseInt(input.substring(0, 1)) * (buttons.length-1);
		int second = Integer.parseInt(input.substring(1, 2)) - 1;
		return first + second;
	}
	
	protected void initButtonPanel() {
		buttons = new Button[10];
		Panel tp = new Panel(new GridLayout(4, 3));
		for(int i = 0; i < buttons.length-1; i++) {
			buttons[i] = new Button();
			buttons[i].setLabel(String.valueOf(i + 1));
			buttons[i].setFont(new Font("Helvetica", Font.BOLD, 14));
			buttons[i].setBackground(Color.white);
			buttons[i].addActionListener(new DrinkSelectionListener(this));
			tp.add(buttons[i]);
		}
		buttons[buttons.length-1] = new Button();
		buttons[buttons.length-1].setLabel(String.valueOf(0));
		buttons[buttons.length-1].setFont(new Font("Helvetica", Font.BOLD, 14));
		buttons[buttons.length-1].setBackground(Color.white);
		buttons[buttons.length-1].addActionListener(new DrinkSelectionListener(this));
		tp.add(buttons[buttons.length-1]);
		this.add(tp);
	}
	
	public void doActionPerformed(ActionEvent e) {
		String strCommand = ((Button) e.getSource()).getActionCommand();
		if(previousCommand.length() == 0) {
			previousCommand = strCommand;
		} else if(previousCommand.length() == 1) {
			previousCommand += strCommand;
			int index = stringToInt(previousCommand);
			if(index >= 0 && index < drinkId.length && drinkSelection[index].getItemState()) {
				transactionController.startTransaction(index);
			}
			previousCommand = "";
		}
	}
		

	/* private class for drink selection */
	private static class NumericDrinkSelection extends DrinkSelection {

		@SuppressWarnings("unused")
		public final static Color IN_STK = Color.GRAY;

		@SuppressWarnings("unused")
		public final static Color OUT_STK = Color.RED;

		private TextField drinkNumber;
		private TextField drinkName;
		private TextField drinkPrice;
		private TextField drinkStatus;

		public NumericDrinkSelection() {
			drinkNumber = new TextField();
			drinkNumber.setEditable(false);
			
			drinkName = new TextField();
			drinkName.setEditable(false);

			drinkPrice = new TextField("     ");
			drinkPrice.setEditable(false);

			drinkStatus = new TextField("Out Of Stock");
			drinkStatus.setEditable(false);

			this.setLayout(new GridLayout(1, 4));
			Panel tp = new Panel(new GridLayout(1, 3));
			tp.add(drinkNumber);

			tp.add(drinkName);
			Panel tp1 = new Panel();
			tp1.add(drinkPrice);
			Panel tp2 = new Panel();
			tp2.add(drinkStatus);
			this.add(tp);
			this.add(tp1);
			this.add(tp2);
		}

		public void setItemState(boolean active) {
			super.setItemState(active);
			drinkName.setEnabled(active);
			drinkStatus.setText(active ? "Available" : "Out Of Stock");
			drinkStatus.setEnabled(!active);
		}
		
		public boolean getItemState() {
			return super.getItemState();
		}

		public void setName(String name) {
			super.setName(name);
			drinkName.setText(name);
		}

		public void setPrice(int price) {
			super.setPrice(price);
			drinkPrice.setText(MoneyFormatter.toMoney(price));
		}
		
		public void setQuantity(int quantity)
		{
			super.setQuantity(quantity);
		}
		
		public void setItemActive(boolean active) {
			drinkName.setEnabled(active);
			drinkStatus.setEnabled(active);
		}
		
		public void setNumber(String number){
			drinkNumber.setText(number);
		}

	}

}
